package com.kk.transactional.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kk.transactional.entity.Order;
import com.kk.transactional.mapper.OrderMapper;
import com.kk.transactional.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author little_lunatic
 * @since 2025-06-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    OrderMapper orderMapper;


    @Autowired
    PersonServiceImpl personService;

    @Transactional(rollbackFor = Exception.class)
    public void add() {
        Order order = new Order();
        order.setGoods("goods1");
        this.save(order);

        try {
            personService.add();
        } catch (Exception e) {

        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void add2() {
        Order order = new Order();
        order.setGoods("goods2");
        this.save(order);

        try {
            personService.add2(); // 全部回滚
        } catch (Exception e){}
    }

    @Transactional(rollbackFor = Exception.class)
    public void add3() {
        Order order = new Order();
        order.setGoods("goods3");
        this.save(order);

        personService.add3();
        throw new RuntimeException(); //全部回滚
    }

    @Transactional(rollbackFor = Exception.class)
    public void add4() {
        Order order = new Order();
        order.setGoods("goods4");
        this.save(order);

        personService.add4();
        throw new RuntimeException(); //只回滚父事务
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add44() {
        Order order = new Order();
        order.setGoods("goods44");
        orderMapper.insert(order);
        try {
            personService.add44();
        } catch (Exception e) {
//            throw new RuntimeException(e);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void add5() {
        Order order = new Order();
        order.setGoods("goods5");
        this.save(order);

        personService.add5();
        throw new RuntimeException(); // 没有事务，不回滚
    }

    @Transactional(rollbackFor = Exception.class)
    public void add6() {
        Order order = new Order();
        order.setGoods("goods6");
        this.save(order);

        personService.add6(); // 有父事务抛异常
    }

    @Transactional(rollbackFor = Exception.class)
    public void add7() {
        Order order = new Order();
        order.setGoods("goods7");
        this.save(order);

        personService.add7();
        throw new RuntimeException(); //外层事务回滚，影响内层事务
    }

    @Transactional(rollbackFor = Exception.class)
    public void add8() {
        Order order = new Order();
        order.setGoods("goods8");
        this.save(order);

        try {
            personService.add8(); //内层事务回滚，不影响外层事务
        } catch (Exception e){}

    }
}
